package com.example.demo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.PunchRecord;
import com.example.demo.model.ResultMsg;
import com.example.demo.service.WeLinkIntegrationService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class WeLinkIntegrationServiceImpl implements WeLinkIntegrationService {
    @Value("${we.link.api-url}")
    private String apiURL;

    @Value("${we.link.api-key}")
    private String apiKey;

    @Value("${we.link.api-secret}")
    private String apiSecret;
    private static final long EXPIRATION_TIME = 60 * 60 * 1000; // 有效期为1小时

    public void sendToWeLink(PunchRecord record) throws IOException, InterruptedException {
//        // 构建请求体
//        Map<String, Object> requestBody1 = new HashMap<>();
//        requestBody1.put("employeeId", record.getUserAccount());
//        requestBody1.put("punchTime", record.getPunchCardTime().toString());
////        requestBody.put("punchType", record.getType());
//
//        ResultMsg resultMsg1 = new ResultMsg();
//        HttpPost httpPost1 = new HttpPost("https://example.com/apigetToken");
//        httpPost1.addHeader("Content-Type", "application/json");
//        httpPost1.addHeader("Authorization", "");
//
//        String json = "{\"key\":\"value\"}";
//        StringEntity entity = new StringEntity(json, "UTF-8");
//        httpPost1.setEntity(entity);
//
//            HttpResponse response = httpClient1.execute(httpPost1);
//            HttpEntity responseEntity = response.getEntity();
//
//                String result = EntityUtils.toString(responseEntity, "UTF-8");
//                System.out.println(result);
//
//                JSONObject jsonObject = JSON.parseObject(result);
//
//        // 构建请求体
//        Map<String, Object> requestBody = new HashMap<>();
//        requestBody.put("employeeId", record.getUserAccount());
//        requestBody.put("punchTime", record.getPunchCardTime().toString());
////        requestBody.put("punchType", record.getType());
//
//        ResultMsg resultMsg = new ResultMsg();
//
//        HttpPost httpPost = new HttpPost("https://example.com/synchronizeAttenceData");
//        httpPost.addHeader("Content-Type", "application/json");
//        httpPost.addHeader("Authorization", jsonObject1.getString("access_token"));
//
//        String json = "{\"key\":\"value\"}";
//        StringEntity entity = new StringEntity(json, "UTF-8");
//        httpPost.setEntity(entity);
//
//        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
//            HttpResponse response = httpClient.execute(httpPost);
//            HttpEntity responseEntity = response.getEntity();
//            if (responseEntity != null) {
//                String result = EntityUtils.toString(responseEntity, "UTF-8");
//                System.out.println(result);
//
//                JSONObject jsonObject = JSON.parseObject(result);
//
//                if (!"0".equals(jsonObject.getString("code"))) {//判断是否获取token信息成功
//                    resultMsg.setSuccess(true);
//                    resultMsg.setData(result);
//                } else {
//                    resultMsg.setSuccess(false);
//                    resultMsg.setCode(jsonObject.getString("code"));
//                    resultMsg.setMsg(jsonObject.getString("msg"));
//                }
//            }
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

    //令牌设置
    private static String generateAccessToken(String employeeId) {
        // 生成随机的UUID作为令牌的一部分
        String tokenId = UUID.randomUUID().toString();

        // 当前时间
        Date now = new Date();
        // 设置过期时间为当前时间加上有效时长
        Date expirationTime = new Date(now.getTime() + EXPIRATION_TIME);

        // 构造令牌字符串，这里简单地拼接了一些信息，实际应用中应该使用加密算法和标准格式
        String token = tokenId + ":" + employeeId + ":" + expirationTime.getTime();
        return token;
    }

    public static void main(String[] args) {
        // 假设用户ID为"123"
        String accessToken = generateAccessToken("123");
        System.out.println("Generated Access Token: " + accessToken);
    }

}